package com.ruibang.glass.equipment.controller;


import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruibang.glass.equipment.constant.Constants;
import com.ruibang.glass.equipment.domain.req.WareHouseManagementReq;
import com.ruibang.glass.equipment.domain.req.device.DeviceAllPageReq;
import com.ruibang.glass.equipment.entity.device.InventoryManagement;
import com.ruibang.glass.equipment.entity.device.OutBoundManagement;
import com.ruibang.glass.equipment.entity.device.SparePartsInventory;
import com.ruibang.glass.equipment.entity.device.WareHouseManagement;
import com.ruibang.glass.equipment.service.InventoryManagementService;
import com.ruibang.glass.equipment.service.OutBoundManagementService;
import com.ruibang.glass.equipment.service.SparePartsInventoryService;
import com.ruibang.glass.equipment.service.WareHouseManagementService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 仓库管理
 * </p>
 *
 * @author mameng
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/api/equipment/ware/house/management")
@Api(value = "仓库管理")
public class WareHouseManagementController {
    @Autowired
    private WareHouseManagementService wareHouseManagementService;

    @Autowired
    private SparePartsInventoryService sparePartsInventoryService;

    @Autowired
    private OutBoundManagementService outBoundManagementService;

    @Autowired
    private InventoryManagementService inventoryManagementService;

    @PostMapping("list")
    @ApiOperation(value = "仓库管理列表", response = ResultBody.class)
    public ResultBody getBoilerList(@RequestBody WareHouseManagementReq managementReq) {
        return ResultBody.success(wareHouseManagementService.queryPage(managementReq));
    }

    @PostMapping("addOrUpdateCheck")
    @ApiOperation(value = "修改添加", response = ResultBody.class)
    public ResultBody addChild(@RequestBody WareHouseManagement repairWorkOrder) {
        return ResultBody.success(wareHouseManagementService.updateOrAddById(repairWorkOrder));
    }

    //
    @PostMapping("deleteAll")
    @ApiOperation(value = "删除", response = ResultBody.class)
    public ResultBody deleteAll(@RequestBody WareHouseManagement wareHouseManagement) {
        long count = sparePartsInventoryService.count(Wrappers.<SparePartsInventory>lambdaQuery().in(SparePartsInventory::getWarehouseId, wareHouseManagement.getIds()));
        if (count > 0) {
            return ResultBody.success("仓库被使用!暂时无法删除");
        }
        long count1 = outBoundManagementService.count(Wrappers.<OutBoundManagement>lambdaQuery().in(OutBoundManagement::getWarehouseId, wareHouseManagement.getIds()));
        if (count1 > 0) {
            return ResultBody.success("仓库被使用!暂时无法删除");
        }

        long count2 = inventoryManagementService.count(Wrappers.<InventoryManagement>lambdaQuery().in(InventoryManagement::getWarehouseId, wareHouseManagement.getIds()));
        if (count2 > 0) {
            return ResultBody.success("仓库被使用!暂时无法删除");
        }

        if (CollectionUtils.isNotEmpty(wareHouseManagement.getIds())) {
            wareHouseManagementService.lambdaUpdate().in(WareHouseManagement::getId, wareHouseManagement.getIds()).set(WareHouseManagement::getIsDelete, Constants.ONE_STR).update();
        }
        if (StringUtils.isNoneBlank(wareHouseManagement.getId())) {
            wareHouseManagementService.lambdaUpdate().eq(WareHouseManagement::getId, wareHouseManagement.getId()).set(WareHouseManagement::getIsDelete, Constants.ONE_STR).update();
        }
        return ResultBody.success();
    }


    @PostMapping("getOne")
    @ApiOperation(value = "查看详情", response = ResultBody.class)
    public ResultBody getOne(@RequestBody WareHouseManagement repairWorkOrder) {
        return ResultBody.success(wareHouseManagementService.getById(repairWorkOrder.getId()));
    }


    @GetMapping("getWareList")
    @ApiOperation(value = "查看详情", response = ResultBody.class)
    public ResultBody getWareList() {
        return ResultBody.success(wareHouseManagementService.getWareList());
    }


}
